(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('CategoryController', CategoryController);

    CategoryController.$inject = ['$scope', '$state','$http', 'Category', 'ParseLinks', 'AlertService', 'paginationConstants', 'pagingParams'];

    function CategoryController ($scope, $state, $http,Category, ParseLinks, AlertService, paginationConstants, pagingParams) {
        var vm = this;

        vm.loadPage = loadPage;
        vm.predicate = pagingParams.predicate;
        vm.reverse = pagingParams.ascending;
        vm.transition = transition;
        vm.itemsPerPage = paginationConstants.itemsPerPage;
        vm.categories={};
        vm.categorymap={};
        loadAll();

        function loadAll () {
            Category.query({
                page: pagingParams.page - 1,
                size: vm.itemsPerPage,
                sort: sort()
            }, onSuccess, onError);
            function sort() {
                var result = [vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc')];
                if (vm.predicate !== 'id') {
                    result.push('id');
                }
                return result;
            }
            function onSuccess(data, headers) {
                vm.links = ParseLinks.parse(headers('link'));
                vm.totalItems = headers('X-Total-Count');
                vm.queryCount = vm.totalItems;
                vm.categories = data;
                vm.page = pagingParams.page;
            }
            function onError(error) {
                AlertService.error(error.data.message);
            }
        }

        function loadPage(page) {
            vm.page = page;
            vm.transition();
        }

        function transition() {
            $state.transitionTo($state.$current, {
                page: vm.page,
                sort: vm.predicate + ',' + (vm.reverse ? 'asc' : 'desc'),
                search: vm.currentSearch
            });
        }
        vm.onfile=function($files,categoryId){
        	for (var i = 0; i < $files.length; i++) {        		
        		var fd = new FormData();
        	    //Take the first selected file
        	    fd.append("jyfile", $files[i]);     	      
      	    		$http({
        	            method: 'post',
        	            url: '/upload/category/'+categoryId,
        	            data:fd,
        	            headers: {'Content-Type': undefined},
        	            transformRequest: angular.identity
        	        }).success(function(ret) {
        	            if (ret.success) {
        	            	//vm.leaguer.idPicture=ret.path;
        	            }else if(!ret.success){
        	            }
        	        })
        		//only one file
        		break;
        	}
        }
        
        function loadCategories(){
        	$http({
        		method: 'get',
        		url: '/api/allcategories',
        		transformRequest: angular.identity
        	}).success(function(ret){
        		vm.categories=ret;
        		for(var i=0; i<vm.categories.length; i++) {
        			vm.categorymap[vm.categories[i].id]=vm.categories[i].name;
        		}
        	});
        }
        
        loadCategories();
    }
})();
